package com.cfjjc.gz.common.listener;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

import org.apache.log4j.Logger;

import com.cfjjc.gz.common.consts.SystemConst;
import com.cfjjc.gz.usraccount.vo.A_USRVO;

public class OnlineUserListener implements HttpSessionListener {

	/**
	 * logger对象
	 */
	private static final Logger logger = Logger.getLogger(OnlineUserListener.class);
	
	/**
	 * 存放在线用户的session列表。
	 */
	public static Map sessionMap = new HashMap();

	public void sessionCreated(HttpSessionEvent event) {
		
	}

	public void sessionDestroyed(HttpSessionEvent event) {
		logger.debug("#########################Destroy session####################");
		HttpSession session = event.getSession();
		
		A_USRVO usr = (A_USRVO)session.getAttribute(SystemConst.SESSION_USER);
		// 判断当前session user是否有值
		if (usr != null) {
			String usrId = usr.getUsr_id();
			HttpSession onLineSession = (HttpSession)sessionMap.get(usrId);
			if (onLineSession != null) {
				onLineSession.invalidate();
			}
			sessionMap.remove(usr.getUsr_id());
		}
	}
}
